package org.rabbit.config;

import javax.annotation.Resource;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration    //相当于spring中的配置文件
public class RabbitmqConfig {

	@Resource
	private UserRabbitMqConfirm urc;
	
	@Bean    //bean标签
    //创建一个队列 
	//绑定默认模式：direct
	public Queue queue() {
		return new Queue("hello");
	}
	
	@Bean
	//绑定到fanout模式  队列
	public Queue queueFanout1() {		
		return new Queue("fanoutqueue1");
	}
	
	@Bean
	//绑定到fanout模式  队列
	public Queue queueFanout2() {		
		return new Queue("fanoutqueue2");
	}
	
	//创建一个fanout广播模式的交换机
	@Bean
	public FanoutExchange fanoutExchange() {
		return new FanoutExchange("fanoutExchage");
	}
	
	//队列绑定到交换机
	@Bean
	public Binding binding() {
		return BindingBuilder.bind(queueFanout1()).to(fanoutExchange());
	}
	
	@Bean
	public Binding binding2() {
		return BindingBuilder.bind(queueFanout2()).to(fanoutExchange());
	}
	
	//对参数进行注入
	@Resource
	public void setConfirmBack(RabbitTemplate rt) {
		rt.setConfirmCallback(urc);  //指定发送到交换机的回调
		rt.setReturnCallback(urc);    //指定发送到queue失败的回调
	}
}

